package models

import (
	"lidongyooo/search-bot/pkg/db"
	"strconv"
	"strings"
)

type Chat struct {
	BaseModel

	ChatId int    `gorm:"column:chat_id"`
	Name   string `gorm:"column:name"`
	Type   string `gorm:"column:type"`
	Status uint8  `gorm:"column:status"`
}

func (c *Chat) Actively() bool {
	return c.Status == 1 || c.Status == 2
}

func (c *Chat) FullActively() bool {
	return c.Status == 1
}

func GetChatByChatId(chatId int) (*Chat, error) {
	chat := &Chat{}
	res := db.MySQL.Where("chat_id", GetOriginChatId(chatId)).First(chat)
	if res.Error != nil {
		return nil, res.Error
	}

	return chat, nil
}

func GetOriginChatId(chatId int) int {
	s := ""
	if chatId < 0 {
		s = strconv.Itoa(chatId)
		if strings.HasPrefix(s, "-100") {
			id, _ := strconv.Atoi(s[4:])
			return id
		} else if strings.HasPrefix(s, "-10") {
			id, _ := strconv.Atoi(s[3:])
			return id
		}
	}

	return chatId
}
